//
//  HostAttentionCell.m
//  SuperRadio
//
//  Created by Damon on 14/12/17.
//  Copyright (c) 2014年 三个臭皮匠. All rights reserved.
//

#import "HostAttentionCell.h"
#import "HostAttentionDetail.h"
#import "UIImageView+WebCache.h"

@interface HostAttentionCell ()

@property(nonatomic, retain) UIImageView *hostImageView;
@property(nonatomic, retain) UILabel *hostName;
@property(nonatomic, retain) UIImageView *vipImageView;
@property(nonatomic, retain) UILabel *sounds;
@property(nonatomic, retain) UILabel *fans;
@property(nonatomic, retain) UILabel *intro;

@end

@implementation HostAttentionCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.hostImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.hostImageView];
        [_hostImageView release];
        
        self.hostName = [[UILabel alloc] init];
        [self.contentView addSubview:self.hostName];
        [_hostName release];
        
        self.vipImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.vipImageView];
        [_vipImageView release];
        
        self.sounds = [[UILabel alloc] init];
        self.sounds.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.sounds];
        [_sounds release];
        
        self.fans = [[UILabel alloc] init];
        self.fans.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.fans];
        [_fans release];
        
        self.intro = [[UILabel alloc] init];
        self.intro.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.intro];
        [_intro release];
        
    }
    return self;
}

-(void)setHostAttention:(HostAttentionDetail *)hostAttention
{
    if (_hostAttention != hostAttention) {
        [_hostAttention release];
        _hostAttention = [hostAttention retain];
    }
    
    // 播主头像赋值
    if (hostAttention.smallLogo.length == 0) {
        // 如果请求的数据没有头像的地址 就赋给默认的图片.
        NSString *path1 = [[NSBundle mainBundle] pathForResource:@"listen" ofType:@"jpg"];
        self.hostImageView.image = [[UIImage imageWithContentsOfFile:path1] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    } else {
        
        [self.hostImageView setImageWithURL:[NSURL URLWithString:[hostAttention.smallLogo stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    }
    
    // 给播主姓名赋值
    self.hostName.text = hostAttention.nickname;
    // 给声音赋值
    if ([hostAttention.tracks floatValue] > 10000) {
        
        CGFloat fans = [hostAttention.tracks floatValue] / 10000;
        self.sounds.text = [NSString stringWithFormat:@"声音 %0.2lf 万", fans];
    } else {
        self.sounds.text = [NSString stringWithFormat:@"声音 %@", [hostAttention.tracks description]];
    }
    
    // 给粉丝数赋值
    if ([hostAttention.followers floatValue] > 10000) {
        
        CGFloat fans = [hostAttention.followers floatValue] / 10000;
        self.fans.text = [NSString stringWithFormat:@"粉丝数 %0.2lf 万", fans];
    } else {
        self.fans.text = [NSString stringWithFormat:@"粉丝数 %@", [hostAttention.followers description]];
    }
    // 认证图片的赋值
    NSString *path = [[NSBundle mainBundle] pathForResource:@"vip" ofType:@"png"];
    self.vipImageView.image = [[UIImage imageWithContentsOfFile:path] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.intro.text = hostAttention.ptitle;
    
    

}


-(void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat width = self.contentView.frame.size.width / 5;
    CGFloat height = self.contentView.frame.size.height / 3;
    CGFloat y = self.contentView.frame.origin.y;
    
    // 播主头像 设置为圆形
    self.hostImageView.frame = CGRectMake(5, y + 10, width, width);
    self.hostImageView.layer.cornerRadius = self.hostImageView.frame.size.height / 2;
    self.hostImageView.layer.masksToBounds = YES;
    // 播主名称
    self.hostName.frame = CGRectMake(width + 10, y + 10, width * 2, height);
    
    // 发表的声音
    self.sounds.frame = CGRectMake(width + 10, height, width, height);
    // 粉丝数
    self.fans.frame = CGRectMake(width * 2 + 20, height, width * 2, height);
    // 博主的简介
    self.intro.frame = CGRectMake(width + 10, height * 2, width * 4, height);
    // 让播主名字自适应
    [self.hostName sizeToFit];
    // 认证的图片
    self.vipImageView.frame = CGRectMake(self.hostName.frame.origin.x + self.hostName.frame.size.width, self.contentView.frame.origin.y + 10, self.hostName.frame.size.height, self.hostName.frame.size.height);
}



- (void)dealloc
{
    [_hostImageView release];
    [_hostName release];
    [_fans release];
    [_vipImageView release];
    [_sounds release];
    [_intro release];
    
    [super dealloc];
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
